package com.hogocloud.corelibrary.common.glide

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.net.Uri
import android.text.TextUtils
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.Priority
import com.bumptech.glide.request.RequestOptions
import com.hogocloud.corelibrary.LibConfig
import java.io.File
import java.util.concurrent.ExecutionException

/**
 * 加载图片 && 基于Glide4.0 &&
 * 后期用到再继续添加功能
 *
 * @author gjc
 * @version 1.0.0
 * @since 2018-04-02
 */

class ImageLoader {

    fun loadThumbnail(context: Context, resize: Int, placeholder: Drawable, imageView: ImageView, uri: Uri) {
        Glide.with(context)
            .asBitmap() // some .jpeg files are actually gif
            .load(uri)
            .apply(
                RequestOptions()
                    .override(resize, resize)
                    .placeholder(placeholder)
                    .centerCrop()
            )
            .into(imageView)
    }

    fun loadGifThumbnail(
        context: Context, resize: Int, placeholder: Drawable, imageView: ImageView,
        uri: Uri
    ) {
        Glide.with(context)
            .asBitmap() // some .jpeg files are actually gif
            .load(uri)
            .apply(
                RequestOptions()
                    .override(resize, resize)
                    .placeholder(placeholder)
                    .centerCrop()
            )
            .into(imageView)
    }

    fun loadImage(context: Context, resizeX: Int, resizeY: Int, imageView: ImageView, uri: Uri) {
        Glide.with(context)
            .load(uri)
            .apply(
                RequestOptions()
                    .override(resizeX, resizeY)
                    .priority(Priority.HIGH)
                    .fitCenter()
            )
            .into(imageView)
    }

    fun loadGifImage(context: Context, resizeX: Int, resizeY: Int, imageView: ImageView, uri: Uri) {
        Glide.with(context)
            .asGif()
            .load(uri)
            .apply(
                RequestOptions()
                    .override(resizeX, resizeY)
                    .priority(Priority.HIGH)
                    .fitCenter()
            )
            .into(imageView)
    }

    fun supportAnimatedGif(): Boolean {
        return true
    }

    companion object {

        /**
         * @param imageView 需要加载图片的控件
         * @param filePath  图片的地址
         * @param radius    圆角的弧度
         */
        fun loadImage(imageView: ImageView, filePath: String, radius: Float) {
            val transform = CornerTransform(LibConfig.getAppContext(), radius)
            val drawable = ColorDrawable(Color.GRAY)
            val options = RequestOptions()
                .centerCrop()
                .placeholder(drawable)
                .transform(transform)
            Glide.with(LibConfig.getAppContext())
                .load(filePath)
                .apply(options)
                .into(imageView)
        }

        fun loadImage(imageView: ImageView, filePath: String) {
            val drawable = ColorDrawable(Color.GRAY)
            val options = RequestOptions()
                .centerCrop()
                .placeholder(drawable)
            Glide.with(LibConfig.getAppContext())
                .load(filePath)
                .apply(options)
                .into(imageView)
        }

        fun loadCover(imageView: ImageView, filePath: String, radius: Float) {
            val transform = CornerTransform(LibConfig.getAppContext(), radius)
            val drawable = ColorDrawable(Color.GRAY)
            val options = RequestOptions()
                .frame(1000000)
                .centerCrop()
                .placeholder(drawable)
                .transform(transform)
            Glide.with(LibConfig.getAppContext())
                .load(filePath)
                .apply(options)
                .into(imageView)
        }

        fun loadCover(imageView: ImageView, filePath: String) {
            val drawable = ColorDrawable(Color.GRAY)
            val options = RequestOptions()
                .frame(1000000)
                .centerCrop()
                .placeholder(drawable)
            Glide.with(LibConfig.getAppContext())
                .load(filePath)
                .apply(options)
                .into(imageView)
        }

        fun loadProfileImage(imageView: ImageView, filePath: String) {
            Glide.with(LibConfig.getAppContext())
                .load(filePath)
                .apply(RequestOptions().error(-1))
                .into(imageView)
        }

        fun clear(imageView: ImageView) {
            Glide.with(LibConfig.getAppContext()).clear(imageView)
        }

        fun loadImage(imageView: ImageView, uri: Uri?) {
            if (uri == null) {
                return
            }
            Glide.with(LibConfig.getAppContext())
                .load(uri)
                .apply(RequestOptions().error(-1))
                .into(imageView)
        }

        fun loadImage(filePath: String, url: String) {
            try {
                val file = Glide.with(LibConfig.getAppContext()).asFile().load(url).submit().get()
                val destFile = File(filePath)
                file.renameTo(destFile)
            } catch (e: InterruptedException) {
                e.printStackTrace()
            } catch (e: ExecutionException) {
                e.printStackTrace()
            }

        }

        @Throws(InterruptedException::class, ExecutionException::class)
        fun loadBitmap(imageUrl: String, targetImageSize: Int): Bitmap? {
            return if (TextUtils.isEmpty(imageUrl)) {
                null
            } else Glide.with(LibConfig.getAppContext()).asBitmap()
                .load(imageUrl)
                .apply(RequestOptions().error(-1))
                .into(targetImageSize, targetImageSize)
                .get()
        }
    }
}
